package com.hainabo.mgcmall.api.system;

import com.hainabo.bootside.core.common.ReturnVO;
import com.hainabo.mgcmall.dao.DriversLicenseRespository;
import com.hainabo.mgcmall.model.DriversLicenseInfo;
import com.hainabo.mgcmall.util.DataTablePageUtil;
import com.hainabo.mgcmall.util.JsonUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;

/**
 * Created by guoshihao on 2017/11/29.
 * 驾驶证认证
 */
@Api(value = "/wx/driversLicense")
@Component
@Path("/wx/driversLicense")
@Produces(MediaType.APPLICATION_JSON)
@Slf4j
public class DriversLicenseResource {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    DriversLicenseRespository driversLicenseRespository;

    @POST
    @Path("/list")
    public DataTablePageUtil list(@Context HttpServletRequest request) {
        DataTablePageUtil dataTablePageUtil = new DataTablePageUtil(request);
        String start = request.getParameter("start");
        String length = request.getParameter("length");

        StringBuilder sb = new StringBuilder();
        sb.append("SELECT \n" +
                "  d.*,\n" +
                "  w.nick_name \n" +
                "FROM\n" +
                "  t_drivers_license_info d \n" +
                "  LEFT JOIN t_wx_user w \n" +
                "    ON w.id = d.user_id ");
        String countSql = "SELECT \n" +
                "  COUNT(1) AS totalNum\n" +
                "FROM\n" +
                "  t_drivers_license_info d \n" +
                "  LEFT JOIN t_wx_user w \n" +
                "    ON w.id = d.user_id \n" +
                "    ";
        if (StringUtils.isNotBlank(start) && StringUtils.isNotBlank(length)) {
            sb.append(" limit " + start + "," + length);
        }
        List rows = jdbcTemplate.queryForList(sb.toString());
        dataTablePageUtil.setData(rows);
        int count = Integer.parseInt(jdbcTemplate.queryForList(countSql).get(0).get("totalNum").toString());
        dataTablePageUtil.setRecordsTotal(count);
        dataTablePageUtil.setRecordsFiltered(count);
        return dataTablePageUtil;
    }

    @POST
    @Path("/updateState")
    public ReturnVO updateState(String json) {
        Map<String,Object> param = JsonUtil.toHashMap(json);
        Long id = Long.valueOf(param.get("id").toString());
        int state = (int) param.get("state");
        driversLicenseRespository.updateState(state,id);
        return new ReturnVO(ReturnVO.CODE_SUCCESS);
    }

    @POST
    @Path("/save")
    public ReturnVO save(@Context HttpServletRequest request, DriversLicenseInfo driversLicenseInfo) {

        return new ReturnVO(driversLicenseRespository.save(driversLicenseInfo));
    }

}
